from torchvision import models
from torch import nn
import torch
import sys
sys.path.append("..")
from networks.utils import initialize_weights
from torch.nn import functional as F

def conv1x1(in_planes, out_planes, stride=1):
    return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)

def conv3x3(in_planes, out_planes, stride=1):
    return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False)

class FCN(nn.Module):
    def __init__(self, in_channels=3, pretrained=True):
        super(FCN, self).__init__()
        # Build models
        resnet = models.resnet34(pretrained)  # BasicBlock [3, 4, 6, 3]
        newconv1 = nn.Conv2d(in_channels, 64, kernel_size=7, stride=2, padding=3, bias=False)
        newconv1.weight.data[:, 0:3, :, :].copy_(resnet.conv1.weight.data[:, 0:3, :, :])  # resnet.conv1.weight.shape = torch.size([64, 3, 7, 7])
        self.layer0 = nn.Sequential(newconv1, resnet.bn1, resnet.relu)
        self.maxpool = resnet.maxpool
        self.layer1 = resnet.layer1
        self.layer2 = resnet.layer2
        self.layer3 = resnet.layer3
        self.layer4 = resnet.layer4
        for n, m in self.layer3.named_modules():
            if 'conv1' in n or 'downsample.0' in n:
                m.stride = (1, 1)
        for n, m in self.layer4.named_modules():
            if 'conv1' in n or 'downsample.0' in n:
                m.stride = (1, 1)

        self.head = nn.Sequential(nn.Conv2d(512, 128, kernel_size=1, stride=1, padding=0, bias=False),
                                  nn.BatchNorm2d(128), nn.ReLU())

        # Initial weights
        initialize_weights(self.head)

    def _make_layer(self, block, inplanes, planes, blocks, stride=1):
        downsample = None
        if stride != 1 or inplanes != planes:
            downsample = nn.Sequential(
                conv1x1(inplanes, planes, stride),
                nn.BatchNorm2d(planes)
            )

            layers = []
            layers.append(block(inplanes, planes, stride, downsample))
            self.inplances = planes * block.expansion
            for _ in range(1, blocks):
                layers.append(block(self.inplances, planes))
            return nn.Sequential(*layers)


class ResBlock(nn.Module):
    expansion = 1
    def __init__(self, inplanes, planes, stride=1, downsample=None):
        super(ResBlock, self).__init__()
        self.conv1 = conv3x3(inplanes, planes, stride)
        self.bn1 = nn.BatchNorm2d(planes)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = conv3x3(planes, planes)
        self.bn2 = nn.BatchNorm2d(planes)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        idetity = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)

        if self.downsample is not None:
            idetity = self.downsample(x)

        out += idetity
        out = self.relu(out)

        return out


class FCNet(nn.Module):
    def __init__(self, in_channels=3, num_classes=3):
        super(FCNet, self).__init__()
        self.FCN = FCN(in_channels, pretrained=True)
        self.classifier1 = nn.Conv2d(128, num_classes, kernel_size=1)
        self.classifier2 = nn.Conv2d(128, num_classes, kernel_size=1)

        self.resCD = self._make_layer(ResBlock, 256, 128, 6, stride=1)
        self.classifierCD = nn.Sequential(nn.Conv2d(128, 64, kernel_size=1), nn.BatchNorm2d(64), nn.ReLU(), nn.Conv2d(64, 1, kernel_size=1))

        initialize_weights(self.classifier1, self.classifier2, self.resCD, self.classifierCD)

    def _make_layer(self, block, inplanes, planes, blocks, stride=1):
        downsample = None
        if stride != 1 or inplanes != planes:
            downsample = nn.Sequential(
                conv1x1(inplanes, planes, stride),
                nn.BatchNorm2d(planes) )

        layers = []
        layers.append(block(inplanes, planes, stride, downsample))
        self.inplanes = planes * block.expansion
        for _ in range(1, blocks):
            layers.append(block(self.inplanes, planes))

        return nn.Sequential(*layers)

    def base_forward(self, x):
        x = self.FCN.layer0(x)
        x = self.FCN.maxpool(x)
        x = self.FCN.layer1(x)
        x = self.FCN.layer2(x)
        x = self.FCN.layer3(x)
        x = self.FCN.layer4(x)
        x = self.FCN.head(x)

        return x

    def CD_forward(self, x1, x2):
        b, c, h, w = x1.size()
        x = torch.cat([x1, x2], 1)
        x = self.resCD(x)
        change = self.classifierCD(x)
        return change

    def forward(self, x1, x2):
        x_size = x1.size()

        x1 = self.base_forward(x1)
        x2 = self.base_forward(x2)

        change = self.CD_forward(x1, x2)

        out1 = self.classifier1(x1)
        out2 = self.classifier2(x2)

        return F.interpolate(change, x_size[2:], mode='bilinear'), F.interpolate(out1, x_size[2:], mode='bilinear'), F.interpolate(out2, x_size[2:], mode='bilinear')


class FCFMNet(nn.Module):
    def __init__(self, in_channels=3, num_classes=3):
        super(FCFMNet, self).__init__()
        self.FCN = FCN(in_channels, pretrained=True)
        self.classifier1 = nn.Conv2d(128, num_classes, kernel_size=1)
        self.classifier2 = nn.Conv2d(128, num_classes, kernel_size=1)

        self.resCD = self._make_layer(ResBlock, 256, 128, 6, stride=1)
        self.classifierCD = nn.Sequential(nn.Conv2d(128, 64, kernel_size=1), nn.BatchNorm2d(64), nn.ReLU(), nn.Conv2d(64, 1, kernel_size=1))

        initialize_weights(self.classifier1, self.classifier2, self.resCD, self.classifierCD)

    def _make_layer(self, block, inplanes, planes, blocks, stride=1):
        downsample = None
        if stride != 1 or inplanes != planes:
            downsample = nn.Sequential(
                conv1x1(inplanes, planes, stride),
                nn.BatchNorm2d(planes) )

        layers = []
        layers.append(block(inplanes, planes, stride, downsample))
        self.inplanes = planes * block.expansion
        for _ in range(1, blocks):
            layers.append(block(self.inplanes, planes))

        return nn.Sequential(*layers)

    def base_forward(self, x):
        x0 = self.FCN.layer0(x)
        x = self.FCN.maxpool(x0)
        x1 = self.FCN.layer1(x)
        x2 = self.FCN.layer2(x1)
        x3 = self.FCN.layer3(x2)
        x4 = self.FCN.layer4(x3)
        x = self.FCN.head(x4)

        # print('x0.shape', x0.shape)  # [4, 64, 128, 128]
        # print('x1.shape', x1.shape)  # [4, 64, 64, 64]
        # print('x2.shape', x2.shape)  # [4, 128, 32, 32]
        # print('x3.shape', x3.shape)  # [4, 256, 32, 32]
        # print('x4.shape', x4.shape)  # [4, 512, 32, 32]

        return x, [x1, x2, x3]

    def CD_forward(self, x1, x2):
        b, c, h, w = x1.size()
        x = torch.cat([x1, x2], 1)
        x = self.resCD(x)
        change = self.classifierCD(x)
        return change

    def forward(self, x1, x2):
        x_size = x1.size()

        x1, fm1 = self.base_forward(x1)
        x2, fm2 = self.base_forward(x2)

        change = self.CD_forward(x1, x2)
        feature_map = fm1 + fm2    # [enc1_f1, enc1_f2, enc1_f3, enc2_f1, enc2_f2, enc_f3] [0, 3], [1, 4], [2, 5]

        return F.interpolate(change, x_size[2:], mode='bilinear'), feature_map


if __name__ == "__main__":
    image_A = torch.ones((4, 3, 512, 512)).cuda()
    image_B = torch.ones((4, 3, 512, 512)).cuda()
    net = FCNet(3, num_classes=7).cuda()
    out_change, output_A, output_B = net(image_A, image_B)
    # print(output_A.shape, out_change.shape)

    fmnet = FCFMNet(3, num_classes=7).cuda()
    out, feature_map = fmnet(image_A, image_B)
    print(out.shape, len(feature_map))